package com.ruoyi.common.utils;


import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;

import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import lombok.Data;
public class SendSmsUtil {

    public static void sendSms(SendSmsRequest request) {
        String countryCode = request.getCountryCode();
        String phone = request.getPhone();
        String code = request.getCode();
        if (!countryCode.contains("+")) {
            countryCode = "+" + countryCode;
        }
        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户 SecretId 和 SecretKey，此处还需注意密钥对的保密
            // 代码泄露可能会导致 SecretId 和 SecretKey 泄露，并威胁账号下所有资源的安全性。密钥可前往官网控制台 https://console.tencentcloud.com/capi 进行获取
            Credential cred = new Credential("IKIDvgH3iDVdhjjeUAuhJkLnMit47zebQ4Wn ", "QfkgWIuKbqywnaEh2CC6qPbdUDry3zpX");
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            SmsClient client = new SmsClient(cred, "ap-singapore", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            com.tencentcloudapi.sms.v20210111.models.SendSmsRequest req = new com.tencentcloudapi.sms.v20210111.models.SendSmsRequest();
            String[] phoneNumberSet1 = {countryCode + phone};
            req.setPhoneNumberSet(phoneNumberSet1);
            String[] templateParamSet = {code};
            req.setTemplateParamSet(templateParamSet);
            req.setSmsSdkAppId("2400002087");
            req.setSignName("9chat");
            if (countryCode.equals("+86") || countryCode.equals("+852") || countryCode.equals("+853") || countryCode.equals("+886")) {
                req.setTemplateId("2911535");
            } else {
                req.setTemplateId("2911542");
            }

            // 返回的resp是一个SendSmsResponse的实例，与请求对象对应
            SendSmsResponse resp = null;

            resp = client.SendSms(req);
            System.out.println("腾讯云短信调用成功返回信息:" + SendSmsResponse.toJsonString(resp));

        } catch (TencentCloudSDKException ex) {
            System.out.println("腾讯云短信调用错误返回信息:" + ex.toString());

            throw new RuntimeException(ex);
        }
    }

    /**
     * 参数对象
     */
    @Data
    public static class SendSmsRequest {

        private String countryCode;
        /**
         * 电话
         */
        private String phone;

        private String code;

        private String password;

        /**
         * 0注册 1忘记密码
         */
        private Integer type;
    }

    public static void main(String[] args) {
        SendSmsRequest request =new SendSmsRequest();
        request.setCountryCode("+86");
        request.setPhone("15939533705");
        request.setCode("123456");
        sendSms(request);
    }
}
